package com.zhaiweijia.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.zhaiweijia.bean.Article;
import com.zhaiweijia.bean.Category;
import com.zhaiweijia.bean.Channel;
import com.zhaiweijia.bean.Comments;
import com.zhaiweijia.bean.Link;
import com.zhaiweijia.bean.Slide;
import com.zhaiweijia.bean.User;
import com.zhaiweijia.service.ArticleService;
import com.zhaiweijia.service.ChannelService;
import com.zhaiweijia.service.SlideService;
import com.zhaiweijia.util.DateUtil;


@Controller
public class IndexController {
	
	@Autowired
	private ArticleService articleService;
	
	@Autowired
	private SlideService slideService;
	
	@Autowired
	private ChannelService channelService;
	
	/**
	 * 首页展示数据
	 * @param m
	 * @param article
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@RequestMapping({"/","","index"})
	public String index(Model m,Article article,
			@RequestParam(defaultValue = "1") Integer pageNum,
			@RequestParam(defaultValue = "5") Integer pageSize) {
		
		//获取所有的栏目数据
		List<Channel> channels = channelService.getChannels();
		m.addAttribute("channels", channels);
		
		//判断，如果选中了栏目频道，则查询该栏目频道数据，否则显示热点数据
		if(article.getChannelId() == null ) {
			article.setHot(1);//热点
			
		}else {
			//根据栏目频道获取其下所有分类数据
			
			List<Category> categories = channelService.getCategoryByChannelId(article.getChannelId());
			m.addAttribute("categories", categories);
		}
		
		article.setStatus(1);//审核通过
		
		//获取数据
		PageInfo<Article> info = articleService.list(pageNum, pageSize, article);
		
		//轮播图
		List<Slide> slides = slideService.getSlides();
		m.addAttribute("slides", slides);
		
		//右侧边栏查询
		
		//24小时热文
		//设置时间
		Article a24 = new Article();
		
		a24.setCreated(DateUtil.offsetDate(-1));//设置为1天前
		a24.setStatus(1);//审核通过
		
		PageInfo<Article> info24Hot = articleService.list(1, 5, a24);
		m.addAttribute("info24Hot", info24Hot);
		
		//点击量排行榜
		Article ahit = new Article();
		
		ahit.setStatus(1);//审核通过
		
		PageInfo<Article> infoHits = articleService.listByHits(1, 5, ahit);
		m.addAttribute("infoHits", infoHits);
		
		//友情链接
		List<Link> links = articleService.getLinks();
		m.addAttribute("links", links);
		
		
		//将数据传到jsp页面
		m.addAttribute("info", info);
		m.addAttribute("article", article);
		
		return "index/index";
	}
	
	/**
	 * 查询文章详情数据
	 * @param articleId
	 * @param m
	 * @return
	 */
	@RequestMapping("detail")
	public String detail(Integer articleId,Model m,
			@RequestParam(defaultValue = "1")Integer pageNum,
			@RequestParam(defaultValue = "3")Integer pageSize) {
		
		//根据id查询文章数据
		Article article = articleService.getDetailById(articleId);
		
		//查询文章的评论
		PageInfo<Comments> info = articleService.getCommentsByArticleId(articleId,pageNum,pageSize);
		
		//将文章数据返回为jsp
		m.addAttribute("article", article);
		m.addAttribute("info", info);
		
		return "index/detail";
	}
	
	/**
	 * 添加文章点击量
	 * @param id
	 * @return
	 */
	@RequestMapping("addHit")
	@ResponseBody
	public boolean addHit(Integer id) {
		
		//根据id查询文章数据
		Article article = articleService.getDetailById(id);
		
		//如果点击量为空，则设置为1
		if(article.getHits()==null) {
			article.setHits(1);
		}else {
			//如果点击量不为空，则将其值加一
			article.setHits(article.getHits() + 1);
		}
		
		//修改数据库
		return articleService.update(article);
	}
	
	/**
	 * 添加评论
	 * @param comments
	 * @param session
	 * @return
	 */
	@RequestMapping("addComments")
	@ResponseBody
	public boolean addComments(Comments comments,HttpSession session) {
		
		//获取当前登录用户
		User user = (User) session.getAttribute("LOGIN_USER");
		
		//设置用户id和当前时间
		comments.setUserId(user.getId());
		comments.setCreated(new Date());
		
		//将评论添加到数据库
		boolean res1 = articleService.addComments(comments);
		
		//根据id查询文章数据
		Article article = articleService.getDetailById(comments.getArticleId());
		
		//修改评论数据
		if(article.getComments()==null) {
			article.setComments(1);
		}else {
			article.setComments(article.getComments() + 1);
		}
		
		//修改数据库文章的评论数量
		boolean res2 = articleService.update(article);
		
		return res1&&res2;
	}
}
